package com.siwei.mes.service.erp.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.erp.Contract;
import com.siwei.mes.entity.erp.ContractExtraInfo;
import com.siwei.mes.mapper.erp.ContractMapper;
import com.siwei.mes.service.erp.ContractService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * (Contract)表服务实现类
 *
 * @author linzi
 * @since 2024-08-14 22:11:29
 */
@Service
public class ContractServiceImpl implements ContractService {
    @Resource
    private ContractMapper contractMapper;

    @Override
    public PageInfo<Contract> getPage(PageQuery<Contract, Contract> query) {
        Contract searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> contractMapper.getList(searchParam));
    }

    @Override
    public Contract loadByCID(Integer CID) {
        return contractMapper.loadByCID(CID);
    }

    @Override
    public List<Contract> getAll(Contract entity) {
        return contractMapper.getList(entity);
    }

    @Override
    public Map<Integer, Contract> getContractMap(Set<Integer> frwdList) {
        Map<Integer, Contract> contractMap = new HashMap<>();
        if (frwdList.isEmpty()) {
            return contractMap;
        }
        List<Contract> list = contractMapper.selectByFrwdList(frwdList);
        for (Contract contract : list) {
            ContractExtraInfo contractExtraInfo = ContractExtraInfo.jsonToObj(contract.getExtrainfo());
            contract.setContractExtraInfo(contractExtraInfo);
            contractMap.put(contract.getFrwd(), contract);
        }
        return contractMap;
    }


}
